package com.demo.gaoxiao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.gaoxiao.entity.Jiaoshi;
import com.demo.gaoxiao.entity.Shiyanyuan;
import com.demo.gaoxiao.entity.Users;
import com.demo.gaoxiao.entity.vo.JiaoshiVo;
import com.demo.gaoxiao.entity.vo.ShiyanyuanVo;
import com.demo.gaoxiao.mapper.ShiyanyuanMapper;
import com.demo.gaoxiao.qpage.QPage;
import com.demo.gaoxiao.service.IShiyanyuanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.gaoxiao.service.IUsersService;
import com.demo.gaoxiao.util.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 实验员 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-06-06
 */
@Service
public class ShiyanyuanServiceImpl extends ServiceImpl<ShiyanyuanMapper, Shiyanyuan> implements IShiyanyuanService {

    @Autowired
    private IUsersService iSysUserService;


    /**
     * 分页查询分类
     * @param qPage
     * @return
     */
    @Override
    public IPage getPageList(QPage qPage, ShiyanyuanVo obj) {
        IPage iPage = new Page(qPage.getPage(), qPage.getRows());
        List<ShiyanyuanVo> mapList = this.baseMapper.getPageList(iPage,obj);
        return iPage.setRecords(mapList);
    }

    /**
     * 新增
     * @param obj
     * @return
     */
    @Override
    public Result insert(ShiyanyuanVo obj) {
        //验证当前账号是否存在
        QueryWrapper<Users> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("no",obj.getNo());
        if(this.iSysUserService.getOne(queryWrapper)!=null){
            return Result.reError("编号已存在，请重新更换编号");
        }
        Users sysUser=new Users();
        sysUser.setName(obj.getName());
        sysUser.setNo(obj.getNo());
        sysUser.setSex(obj.getSex());
        sysUser.setType(2);
        sysUser.setAge(obj.getAge());
        this.iSysUserService.save(sysUser);
        Shiyanyuan dto=new Shiyanyuan();
        BeanUtils.copyProperties(obj,dto);
        dto.setId(sysUser.getId());
        this.save(dto);
        return Result.reOk("恭喜，新增成功");
    }

    /**
     * 修改
     * @param obj
     * @return
     */
    @Override
    public Result updates(ShiyanyuanVo obj) {
        Shiyanyuan dto=this.getById(obj.getId());
        BeanUtils.copyProperties(obj,dto,new String[]{"id"});
        this.updateById(dto);
        Users sysUser=this.iSysUserService.getById(obj.getId());
        sysUser.setName(obj.getName());
        sysUser.setNo(obj.getNo());
        sysUser.setSex(obj.getSex());
        sysUser.setAge(obj.getAge());
        this.iSysUserService.updateById(sysUser);
        return Result.reOk("恭喜，修改成功");
    }

}
